//
//  FoodListCell.swift
//  eat_known
//
//  Created by 心情 on 2018/8/2.
//  Copyright © 2018年 心情. All rights reserved.
//

import Foundation
import UIKit
import SnapKit

class PhaseButtonGroupView: UIView {
    internal var _phases: [Phase]?
    var phases: [Phase]? {
        set {
            _phases = newValue
            updateLayout()
        }
        get {
            return _phases
        }
    }
    internal var buttons: [UIButton] = []
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    init() {
        super.init(frame: CGRect.zero)
    }
    
    init(_ phases:[Phase]) {
        super.init(frame: CGRect.zero)
        self.phases = phases
        updateLayout()
    }
    
    func updateLayout() {
        _ = buttons.map { (button) -> Void in
            button.removeFromSuperview()
        }
        buttons.removeAll()
        if let _ = phases?.count {
            for phase in phases! {
                let button = UIButton(type: .custom)
                button.imageView?.contentMode = .scaleAspectFit
                button.titleLabel?.font = UIFont.normalFont
                button.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, -5)
                button.setImage(phase.foodState.icon(), for: .normal)
                button.setTitle(phase.state.rawValue, for: .normal)
                button.setTitleColor(UIColor.darkText, for: .normal)
                addSubview(button)
                buttons.append(button)
            }
        }
        setNeedsLayout()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let margin = CGFloat(0)
        let padding = CGFloat(10)
        let width = (frame.size.width - CGFloat(buttons.count - 1) * padding - margin * 2) / CGFloat(buttons.count)
        for (index, button) in buttons.enumerated() {
            button.frame = CGRect(x: margin + CGFloat(index) * padding + width * CGFloat(index),
                                  y: 0,
                                  width: width,
                                  height: frame.size.height)
        }
    }
    
}


class FoodListCell: UITableViewCell {
    var iconView: UIImageView
    var titleLabel: UILabel
    var subTitleLabel: UILabel
    var buttonGroupView: PhaseButtonGroupView
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        iconView = UIImageView()
        titleLabel = UILabel()
        subTitleLabel = UILabel()
        buttonGroupView = PhaseButtonGroupView()

        super.init(style: style, reuseIdentifier: reuseIdentifier)

        contentView.addSubview(iconView)
        iconView.contentMode = .scaleAspectFill
        iconView.clipsToBounds = true
        
        contentView.addSubview(titleLabel)
        titleLabel.font = UIFont.boldFont
        titleLabel.textColor = UIColor.darkText
        titleLabel.textAlignment = .left
        
        contentView.addSubview(subTitleLabel)
        subTitleLabel.font = UIFont.normalFont
        subTitleLabel.textColor = UIColor.darkGray
        subTitleLabel.textAlignment = .left
        subTitleLabel.numberOfLines = 2

        contentView.addSubview(buttonGroupView)
        
        iconView.snp.makeConstraints { (make) in
            make.top.left.bottom.equalToSuperview().inset(10)
            make.width.equalTo(iconView.snp.height)
        }
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(iconView.snp.top)
            make.right.equalToSuperview().inset(10)
            make.left.equalTo(iconView.snp.right).offset(10)
        }
        subTitleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(titleLabel)
            make.top.equalTo(titleLabel.snp.bottom).offset(5)
            make.right.equalToSuperview().inset(10)
        }
        buttonGroupView.snp.makeConstraints { (make) in
            make.top.equalTo(subTitleLabel.snp.bottom).offset(5)
            make.height.equalTo(15)
            make.left.equalTo(iconView.snp.right).offset(10)
            make.right.equalToSuperview().inset(10)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        iconView = UIImageView()
        titleLabel = UILabel()
        subTitleLabel = UILabel()
        buttonGroupView = PhaseButtonGroupView()

        super.init(coder: aDecoder)
    }
}
